<#macro edit url='' itemList=[]>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../layui/layui.js"></script>
<link rel="stylesheet" href="../layui/css/layui.css" media="all">
<script>
layui.use(['form', 'layer', 'upload', 'laydate'], function () {
	var form = layui.form,
		layer = layui.layer,
		upload = layui.upload,
		laydate = layui.laydate;
	var index = parent.layer.getFrameIndex(window.name);// 获取当前子窗口索引
	form.on("submit(centerform)", function(data){
		<#if itemList?size gt 0>
			<#list itemList as il>
				<#if il.type?? && il.type == 'radio' && il.verify == 'required'>
					if(!data.field.${il.name}) {
						layer.msg("${il.title}不能为空！", {icon: 5});
						return false;
					}
				</#if>
			</#list>
		</#if>
		layer.load();// 进度转动动画
		$.ajax({
			url: "${url!''}",
			data: data.field,
			type: "post",
			dataType: "json",
			success: function (result) {
				layer.closeAll('loading');// 关闭进度转动动画
				parent.layer.msg(result.message, {time:1000}, function(){
					if(result.status == "success") {
						parent.layer.close(index);// 关闭当前窗口
					} else if(result.status == "nologin") {
						layer.msg(result.message + "<br/>页面3s后跳转登录", {
							time: 3000
						}, function() {
							top.location.href = location.href
						});
					}
				});
			},
			error: function(xhr,status,error) {
				layer.closeAll('loading');// 关闭进度转动动画
				console.log(xhr);
				console.log(status);
				console.log(error);
			}
		});
		return false;
	});

	<#if itemList?size gt 0>
		<#list itemList as il>
			<#if il.type??>
				<#if il.type == 'file'>
					<#if il.eleList??>
						<#list il.eleList as el>
							var upload_${il.name} = upload.render({
								elem: '#${il.name}', //绑定元素
								url: '${el.url!''}', //上传接口
								size: '${el.size!'1024'}',
								accept: '${el.accept!'file'}',
								acceptMime: '${el.accept2!'images'}',
								field: '${il.name}File',
								before: function(obj) {
									layer.load();// 进度转动动画
								},
								done: function(result, index, upload){
									layer.closeAll('loading');// 关闭进度转动动画
									if(result.status == "success") {
										$("input[name='${il.name}']").val(result.data);
										<#if el.accept?? && el.accept == 'images'>
											$("#${il.name}_img").attr("src", "http://images.kaola100.com/"+result.data);
										</#if>
									} else {
										layer.alert(result.message, {icon: 5});
									}
								},
								error: function(){
									layer.closeAll('loading');// 关闭进度转动动画
								}
							});
							<#break/>
						</#list>
					</#if>
				<#elseif il.type == 'date'>
					<#if il.eleList??>
						<#list il.eleList as el>
							laydate.render({ 
							  elem: '#${il.name}',
							  type: '${el.type}'
							});
							<#break/>
						</#list>
					</#if>
				</#if>
			</#if>
		</#list>
	</#if>

	<#if itemList?size gt 0>
		form.verify({
			<#list itemList as il>
				<#if !il.type?? || il.type != 'hidden' || il.type != 'radio'>
					${il.name}: function(str) {
						str = str.trim();
					
					<#list il.verify?split("|") as v>
						<#--校验非空-->
						<#if v == 'required'>
						if(str.length == 0){
							return '${il.title}不能为空！';
						}
						</#if>
						<#--校验数字-->
						<#if v == 'number'>
						var reg = /^[0-9]*$/;
						if(!reg.test(str)){
							return '${il.title}必须为数字！';
						}
						</#if>
						<#--校验整数-->
						<#if v == 'integer'>
						var reg = /^-?\d+$/;
						if(!reg.test(str)){
							return '${il.title}必须为整数数字！';
						}
						</#if>
						<#--校验正整数-->
						<#if v == 'pinteger'>
						var reg = /^\+?[1-9][0-9]*$/;
						if(!reg.test(str)){
							return '${il.title}必须为正整数数字！';
						}
						</#if>
						<#--校验手机-->
						<#if v == 'mobile'>
						var reg = /^1\d{10}$/;
						if(!reg.test(str)){
							return '${il.title}格式不正确！';
						}
						</#if>
						<#--校验邮箱-->
						<#if v == 'email'>
						var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						if(!reg.test(str)){
							return '${il.title}格式不正确！';
						}
						</#if>
						<#--校验网址-->
						<#if v == 'url'>
						var reg = /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/;
						if(!reg.test(str)){
							return '${il.title}格式不正确！';
						}
						</#if>
						<#--校验日期-->
						<#if v == 'date'>
						var reg = /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/;
						if(!reg.test(str)){
							return '${il.title}格式不正确！';
						}
						</#if>
						<#--校验身份证-->
						<#if v == 'identity'>
						var reg = /(^\d{15}$)|(^\d{17}(x|X|\d)$)/;
						if(!reg.test(str)){
							return '${il.title}格式不正确！';
						}
						</#if>
						<#--校验密码-->
						<#if v == 'password'>
						var reg = /^(?![\d]+$)(?![a-zA-Z]+$)(?![!#$%^&*]+$)[\da-zA-Z!#$%^&*]{6,20}$/;
						if(!reg.test(str)){
							return '密码格式不正确，必须为6-20位包含数字、字母、特殊符号（!#$%^&*）字符组合！';
						}
						</#if>
						<#--校验确认密码-->
						<#if v == 'password2'>
						if(str != $("input[name='password']").val()) {
							return '两次输入密码不一致！';
						}
						</#if>
						<#if v?starts_with('length')>
							<#assign min = v?substring(v?index_of("[")+1,v?index_of(",")) />
							<#if min != '' && min != '0'>
							if(str.length < ${min}) {
								return '${il.title}长度不能小于${min}！';
							}
							</#if>
							<#assign max = v?substring(v?index_of(",")+1,v?index_of("]")) />
							<#if max != '' && max != '0'>
							if(str.length > ${max}) {
								return '${il.title}长度不能大于${max}！';
							}
							</#if>
						</#if>
						<#if v?starts_with('range')>
							<#assign min = v?substring(v?index_of("[")+1,v?index_of(",")) />
							<#if min != ''>
							if(Number(str) < ${min}) {
								return '${il.title}不能小于${min}！';
							}
							</#if>
							<#assign max = v?substring(v?index_of(",")+1,v?index_of("]")) />
							<#if max != ''>
							if(Number(str) > ${max}) {
								return '${il.title}不能大于${max}！';
							}
							</#if>
						</#if>
					</#list>
					}<#if il_index lt itemList?size-1>,</#if>
				</#if>
			</#list>
		});
	</#if>
});
</script>
<form class="layui-form" method="post" style="margin:20px">
	<#if itemList?size gt 0>
		<#list itemList as il>
			<#if !il.type?? || il.type != 'hidden'>
				<div class="layui-form-item">
					<label class="layui-form-label">${il.title}：</label>
					<div class="layui-input-block">
			</#if>
			<#if il.elements == 'input'>
				<#if il.type == 'hidden'>
					<input type="hidden" name="${il.name}" value="${il.value}"/>
				<#elseif il.type == 'password'>
					<input type="password" style="display: none">
					<input type="password" placeholder="请输入${il.title}" name="${il.name}" autocomplete="new-password" class="layui-input" lay-verify="${il.name}">
				<#elseif il.type == 'radio'>
					<#if il.eleList??>
						<#list il.eleList as el>
							<input type="radio" name="${il.name}" value="${el.code}" title="" lay-filter="${il.name}"<#if il.value == el.code> checked</#if>/>
							<div style="display: inline-block;height:28px;line-height:28px;margin:5px 20px 0 -25px;vertical-align: middle;">${el.value}</div>
						</#list>
					</#if>
				<#elseif il.type == 'checkbox'>
					<#if il.eleList??>
						<#list il.eleList as el>
							<input type="checkbox" name="${il.name}" value="${el.code}" title="" lay-filter="${il.name}"<#if il.value == el.code> checked</#if>/>
							<div style="display: inline-block;height:28px;line-height:28px;margin:5px 20px 0 0;vertical-align: middle;">${el.value}</div>
						</#list>
					</#if>
				<#elseif il.type == 'file'>
					<button type="button" class="layui-btn" id="${il.name}">上传${il.title}</button>
					<input type="hidden" name="${il.name}" value="${il.value}"/>
					<#if il.eleList??>
						<#list il.eleList as el>
							<#if el.accept?? && el.accept == 'images'>
								<img id="${il.name}_img" class="layui-upload-img" style="width:50px;"<#if il.value?? && il.value != ''> src="http://images.kaola100.com/${il.value}"</#if>>
							</#if>
							<#break/>
						</#list>
					</#if>
				<#elseif il.type == 'date'>
					<input id="${il.name}" type="text" class="layui-input" placeholder="请输入${il.title}" name="${il.name}" autocomplete="off" value="${il.value}" lay-verify="${il.name}">
				<#else>
					<input type="text" class="layui-input" placeholder="请输入${il.title}" name="${il.name}" autocomplete="off" value="${il.value}" lay-verify="${il.name}">
				</#if>
			<#elseif il.elements == 'textarea'>
				<#if il.eleList??>
					<#list il.eleList as el>
						<textarea<#if el.cols?? && el.cols != '0'> cols="${el.cols}"</#if><#if el.rows?? && el.rows != '0'> rows="${el.rows}"</#if>
							class="layui-textarea" placeholder="请输入${il.title}" name="${il.name}" lay-verify="${il.name}">${il.value}</textarea>
						<#break/>
					</#list>
				</#if>
			<#elseif il.elements == 'select'>
				<select name="${il.name}" lay-filter="${il.name}" lay-verify="${il.name}">
					<option value="">--请选择${il.title}--</option>
					<#if il.eleList??>
						<#list il.eleList as el>
							<option value="${el.code}"<#if il.value?? && il.value == el.code> selected</#if>>${el.value}</option>
						</#list>
					</#if>
				</select>
			</#if>
			<#if !il.type?? || il.type != 'hidden'>
					</div>
				</div>
			</#if>
		</#list>
	</#if>
	<#nested />
	<div class="layui-form-item">
		<div class="layui-input-block">
			<button class="layui-btn" lay-filter="centerform" lay-submit>保存</button>
		</div>
	</div>
</form>
</#macro>